package com.game.ui.systemBoard.componet
{
	import com.game.core.frame.FrameManager;
	import com.game.core.frame.UpdatableSprite;
	
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class DynamicInfoComponet extends UpdatableSprite
	{
		private var _maxStayTime : int;
		private var _infoLen : int;
		private var _infoArr : Array = [];
		private var _txtArr : Array = [];
			
		public function DynamicInfoComponet(width : int , height : int , infoLen : int , maxStayTime : int = 2000)
		{
			super();
			_infoLen = infoLen;
			_maxStayTime = maxStayTime;
			this.x = 450;
			this.y = 600;
			init();
		}
		
		private function init():void
		{
			for(var i : int = 0 ; i < _infoLen ; i++)
			{
				var textField : TextField = new TextField();
//				textField.styleSheet = styleSheet;
				textField.selectable = false;
				textField.antiAliasType = AntiAliasType.ADVANCED;
				textField.autoSize = TextFieldAutoSize.LEFT;
				addChild(textField);
				_txtArr.push(textField);
			}
			this.mouseEnabled = false;
		}
		
		public function addInfo(value : String):void
		{
			if(value == "")
				return;
			
			var dynText : DynamaicText = new DynamaicText(value);
			_infoArr.push(dynText);
			if(_infoArr.length > _infoLen)
			{
				_infoArr.shift();
			}
			show();
		}
		
		private function show():void
		{
			if(!isRegister)
			{
				register();
				frameRate = 10;
				this.visible = true;
			}
		}
		
		private function hide():void
		{
			if(isRegister)
			{
				unRegister();
				this.visible = false;
			}
		}
		
		override public function update(elapsedTime:int):void
		{
			var textField : TextField;
			var dynText : DynamaicText;
			
			for(var i:int = 0;i<_infoLen;i++)
			{
				dynText = _infoArr[i];
				if(!dynText)
					continue;
				
				trace("elapsedTime == "+elapsedTime);
				textField = _txtArr[i];
				textField.htmlText = dynText.text;
				textField.alpha = dynText.alpha;
				textField.x = (width - textField.width) * 0.5;
				textField.y = i * textField.height;
				trace(" textField.alpha == "+textField.alpha);
				
				if(dynText.showTime < _maxStayTime)
				{
					dynText.showTime += elapsedTime;
				}else
				{
					dynText.alpha -= 0.1;
				}
			}
			
			if(textField.alpha <= 0)
				hide();
		}
	}
}

class DynamaicText
{
	public var text : String;
	public var showTime : int;
	public var alpha : Number;
	
	public function DynamaicText(text : String , showTime : int = 0, alpha : Number = 1)
	{
		this.text = text;
		this.showTime = showTime;
		this.alpha = alpha;
	}
}